#pragma once

#include	<Runnable.h>

/**
 * HTTP/HTTPS wrapper
 */
class Http : public IRunnable {
	typedef std::function<void()> ErrorHandler;
	typedef std::function<void(const std::string &)> ResponseHandler;

public:
	Http() : _url(), _param(), _post(false) {}

	/**
	 * Set handler for error.
	 */
	void SetErrorHandler(const ErrorHandler & handler) { _error = handler; }

	/**
	 * Set response handler.
	 */
	void SetResponseHanler(const ResponseHandler handler) { _response = handler; }

	/**
	 * Start HTTP/HTTPS request.
	 *
	 * \param	url		HTTP/HTTPS request url.
	 * \param	param	URL parameter for example : "id=1&name=leo"
	 * \param	post	Using POST instead of GET?
	 */
	void Start(const std::string & url, const std::string & param, bool post = false);

	/**
	 * Jobs running in worker thread.
	 */
	virtual void Run() override;

private:
	static size_t __HandleResponse(void * data, size_t size, size_t count, void * userdata);

private:
	std::string _url;
	std::string _param;
	bool _post;
	ErrorHandler _error;
	ResponseHandler _response;
};